package com.xnfh.field.ui.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.gyf.immersionbar.ktx.immersionBar
import com.xnfh.common.bean.Grade
import com.xnfh.common.bean.SoilMessage
import com.xnfh.common.bean.SoilZdMessage
import com.xnfh.field.R
import com.xnfh.field.databinding.ActivitySoliMessageLayoutBinding
import com.xnfh.field.viewmodel.SoilMessageViewModel
import com.xnfh.framework.base.BaseActivity
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.framework.log.LogUtil
import java.math.BigDecimal

/**
 * @Description:土壤信息
 * @Author: ziheng
 * @CreateDate: 2023/11/29 10:01
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/29 10:01
 * @UpdateRemark: 更新说明
 */
class SoilMessageActivity : BaseActivity<ActivitySoliMessageLayoutBinding>() {
    override fun getLayoutID(): Int = R.layout.activity_soli_message_layout

    companion object {
        fun start(context: Context, bundle: Bundle? = null) {
            val intent = Intent(context, SoilMessageActivity::class.java)
            if (bundle != null) {
                intent.putExtra("bundle", bundle)
            }
            context.startActivity(intent)
        }
    }

    override fun initImmersionBar() {
        immersionBar {
            statusBarDarkFont(true).statusBarColor(R.color.white) //状态栏字体是深色，不写默认为亮色
        }
    }


    private var centerCoordinate: String? = null
    private var viewModel: SoilMessageViewModel? = null

    //智能分析的数据
    private var grades: List<Grade>? = null
    override fun init(savedInstanceState: Bundle?) {
        mBind.soilTitle.headerTitleTxt.text = getString(R.string.soil_message)
        val bundleExtra = intent.getBundleExtra("bundle")
        centerCoordinate = bundleExtra?.getString("centerCoordinate")
        val fieldId = bundleExtra?.getString("fieldId")
        mBind.soilPhLand.setType(1)
        mBind.soilFlzOne.setType(2)
        mBind.soilFlzTwo.setType(3)
        mBind.soilFlzThree.setType(4)
        mBind.soilFlzFour.setType(5)
        viewModel?.requestSoilZdMessage(fieldId, centerCoordinate)

        mBind.soilAiSelect.setOnCheckedChangeListener { group, checkedId ->
            when (group.checkedRadioButtonId) {
                mBind.soilAiRadioOne.id -> {
                    //PH值
                    setAiMessage(5)
                }

                mBind.soilAiRadioTwo.id -> {
                    //有机质
                    setAiMessage(8)
                }

                mBind.soilAiRadioThree.id -> {
                    //有效磷
                    setAiMessage(18)
                }

                mBind.soilAiRadioFour.id -> {
                    //速效钾
                    setAiMessage(16)
                }
            }
        }
    }

    override fun observe() {
        viewModel = SoilMessageViewModel()
        viewModel?.soilZdMessage?.observe(this, object : BaseStateObserver<SoilZdMessage>(null) {
            override fun getRespDataSuccess(it: SoilZdMessage) {
                super.getRespDataSuccess(it)
                //土壤类型
                if (it.nationList.isNotEmpty()) {
                    it.nationList[0].gns5SoilName.let { mBind.soilTypeValue.text = it }
                }
                //海拔
                mBind.soilAltitudeValue.text = String.format("%.2f", it.gg5Dem)
                //坡度
                mBind.soilSlopeValue.text = String.format("%.2f", it.gg5Slope)
                grades = it.grade
                setAiMessage(5)
                setFlzMessage(it)
            }
        })
    }

    /**
     * 设置PH值、肥力值
     */
    private fun setFlzMessage(soilZdMessage: SoilZdMessage) {
        //PH值
        val soil = soilZdMessage.soil
        soil.avgPh.let {
            val avgPh = it.toFloat()
            val phLevel = if (avgPh < 4.5) {
                1
            } else if (avgPh < 5.5) {
                2
            } else if (avgPh < 6.5) {
                3
            } else if (avgPh < 7.5) {
                4
            } else {
                5
            }
            mBind.soilPhLand.setData(phLevel, String.format("%.2f", avgPh))
        }

        soil.avgYjz.let {
            //有机质
            val yjzLevel = if (soil.avgYjz.toFloat() < 10) {
                1
            } else if (soil.avgYjz.toFloat() < 20) {
                2
            } else if (soil.avgYjz.toFloat() < 30) {
                3
            } else if (soil.avgYjz.toFloat() < 40) {
                4
            } else {
                5
            }
            mBind.soilFlzOne.setData(yjzLevel, String.format("%.2f", soil.avgYjz.toFloat()))
        }
        if (soil.avgJjd == null) {
            soil.avgQd.let {
                //全氮
                val qdLevel = if (soil.avgQd.toFloat() < 0.75) {
                    1
                } else if (soil.avgQd.toFloat() < 1) {
                    2
                } else if (soil.avgQd.toFloat() < 1.5) {
                    3
                } else if (soil.avgQd.toFloat() < 2) {
                    4
                } else {
                    5
                }
                mBind.soilFlzTwo.setType(3)
                mBind.soilFlzTwo.setData(qdLevel, String.format("%.2f", soil.avgQd.toFloat()))
            }
        } else {
            val avgJjd = soil.avgJjd!!.toFloat()
            val avgJjdLevel = if (avgJjd < 50) {
                1
            } else if (avgJjd < 100) {
                2
            } else if (avgJjd < 150) {
                3
            } else if (avgJjd < 200) {
                4
            } else {
                5
            }
            mBind.soilFlzTwo.setType(7)
            mBind.soilFlzTwo.setData(avgJjdLevel, String.format("%.2f", avgJjd))
        }

        soil.avgYxl.let {
            //有效磷
            val yxlLevel = if (soil.avgYxl.toFloat() < 5) {
                1
            } else if (soil.avgYxl.toFloat() < 10) {
                2
            } else if (soil.avgYxl.toFloat() < 20) {
                3
            } else if (soil.avgYxl.toFloat() < 30) {
                4
            } else {
                5
            }
            mBind.soilFlzThree.setData(yxlLevel, String.format("%.2f", soil.avgYxl.toFloat()))
        }
        soil.avgSxj.let {
            //速效磷
            val sxlLevel = if (soil.avgSxj.toFloat() < 50) {
                1
            } else if (soil.avgSxj.toFloat() < 100) {
                2
            } else if (soil.avgSxj.toFloat() < 150) {
                3
            } else if (soil.avgSxj.toFloat() < 200) {
                4
            } else {
                5
            }
            mBind.soilFlzFour.setData(sxlLevel, String.format("%.2f", soil.avgSxj.toFloat()))
        }
    }


    /**
     * 设置智能分析数据
     * @param i 5(ph值)
     */
    private fun setAiMessage(i: Int) {
        if (grades == null) {
            LogUtil.e(tag = TAG, message = "grades == null")
            return
        }
        for (item in grades!!) {
            if (item.ID == i) {
                //养分含量
                mBind.soilNutrientNum.text = getString(R.string.soil_nutrient_num) + item.content
                //诊断结果
                mBind.soilDiagnosisResult.text =
                    getString(R.string.soil_diagnosis_result) + item.diagnostic
                //危害诊断
                mBind.soilWhzdValue.text = item.harm
                //常规措施
                mBind.soilCgcsValue.text = item.routineMeasures
                //高效措施
                mBind.soilGxcsValue.text = item.efficientMeasures
            }
        }
    }

}